package com.red.wine.ui;

import java.io.IOException;

import com.google.json.Gson;
import com.google.json.JsonParseException;
import com.red.wine.R;
import com.red.wine.WineApp;
import com.red.wine.model.Headers;
import com.red.wine.model.RegetPassData;
import com.red.wine.model.RegetPassReq;
import com.red.wine.model.RegisterData;
import com.red.wine.model.RegisterReq;
import com.red.wine.model.RegisterReturn;
import com.red.wine.util.IntentUtil;
import com.red.wine.util.StringUtil;
import com.red.wine.webservice.ServiceAsynTask;
import com.red.wine.webservice.ServiceHelper;
import com.red.wine.webservice.ServiceHost;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class RegetPassActivity extends BaseActivity {
	ImageView back;
	private Button btn;
	private EditText email;
	private Context context;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.reget_pass_2);
		context = this;
		back = (ImageView) findViewById(R.id.reget_pass_info_back_2);
		btn= (Button) findViewById(R.id.send_email_to_reget_pass);
		email = (EditText) findViewById(R.id.pur_email_to_reget_pass);
		
		back.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				IntentUtil.redirectToNext(context, LoginActivity.class);
				finish();
			}
		});
		btn.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				if(WineApp.getInstance().isWiFiActive() || WineApp.getInstance().isNetworkAvailable()){
					String getEmail = email.getText().toString().trim();
					if(!StringUtil.isEmptyOrNull(getEmail)){
						if(!StringUtil.checkEmail(getEmail)){
							Toast.makeText(context, "邮箱输入有误", Toast.LENGTH_SHORT).show();
							return;
						}
					}else{
						Toast.makeText(context, "邮箱为空", Toast.LENGTH_SHORT).show();
						return;
					}
					getData(getEmail);
				}else{
					Toast.makeText(context, "无可用网络", Toast.LENGTH_SHORT).show();
				}
			}
		});
	}
	
	private void getData(final String email){
		showLoading();
		new ServiceAsynTask<RegisterReturn>() {

			@Override
			protected RegisterReturn callService() throws IOException,
					JsonParseException, AssertionError {
				RegetPassReq taste = new RegetPassReq();
				Headers header = new Headers();
				header.setUa("ANDROID_HY/2.0.1.0 (customerid=gypsii, variant=gypsii)");

				RegetPassData data = new RegetPassData();
				data.setEmail(email);

				taste.cmd = "user_forgot";
				taste.headers = header;
				taste.data = data;
				taste.sid= "7b8bb3e016fb16f49bc4dd26b7b0ebe632fbe2";

				String up = new Gson().toJson(taste);
				String send = StringUtil.unicodeToUtf8("json=" + up);
				String urlstr = ServiceHost.getTasteURL();

				return ServiceHelper.getInstance().postData(urlstr, send,
						RegisterReturn.class);
			}

			@Override
			protected void runWithResult(RegisterReturn result) throws Exception {
				
				if (result != null) {
					bindData(result);
					Log.e("TAG", "isnotnull");
				} else {
					Log.e("TAG", "is null");
					Toast.makeText(context, "发送失败", Toast.LENGTH_SHORT).show();
				}
				hideLoading();
			}

			@Override
			protected void runWithError(RegisterReturn result, int iErrorCode)
					throws Exception {
				Log.e("TAG", "异常");
				IntentUtil.redirectToNext(context, LoginActivity.class);
				finish();
				hideLoading();
			}
		}.execute();
	}
	
	private void bindData(RegisterReturn result){
		if(result.getRsp().equals("1")){
			Toast.makeText(context, "发送成功,请查收", Toast.LENGTH_SHORT).show();
		}else{
			Toast.makeText(context, "发送失败", Toast.LENGTH_SHORT).show();
		}
		IntentUtil.redirectToNext(context, LoginActivity.class);
		finish();
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			IntentUtil.redirectToNext(context, LoginActivity.class);
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}
}
